package com.haotunet.app.goldtreasure.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.utils.XEditUtils;
import com.haotunet.app.goldtreasure.views.TitleView;

public class RealNameAnthActivity extends BaseActivity {
	private TitleView mTitleView;
	private EditText mRealNameTxt, mIdNumTxt;
	private Button btnSure;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_real_name_auth);
		initView();
	}

	private void initView() {
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText("实名认证");
		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mRealNameTxt = (EditText) findViewById(R.id.et_real_name);
		mIdNumTxt = (EditText) findViewById(R.id.et_id_num);
		XEditUtils.set(mIdNumTxt, Strs.LETTER_NUMBER, "请输入字母或数字");
		btnSure = (Button) findViewById(R.id.btn_sure);
		btnSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String realName = mRealNameTxt.getText().toString();
				String idNum = mIdNumTxt.getText().toString();
				if (TextUtils.isEmpty(realName)) {
					UIHelper.showToast(RealNameAnthActivity.this, "请输入姓名!");
					return;
				}
				if (TextUtils.isEmpty(idNum)) {
					UIHelper.showToast(RealNameAnthActivity.this, "请输入身份证号!");
					return;
				}
				submitAuthInfo(realName, idNum);
			}

		});
	}

	private void submitAuthInfo(final String realName, final String idNum) {
		ActionUtil actionUtil = new ActionUtil(RealNameAnthActivity.this);
		actionUtil.submitAnthInfo(realName, idNum);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (isFinishing()){
					return;
				}
				if (model == null) {
					UIHelper.showToast(RealNameAnthActivity.this, getString(R.string.rank_fail));
					return;
				}
				if (model.getErrorMsg() != null) {
					UIHelper.showToast(RealNameAnthActivity.this, model.getErrorMsg());
				} else {
					if ("true".equals(model.getStatus())) {
						UIHelper.showToast(RealNameAnthActivity.this, "认证成功");
						updateUserInfo(realName, idNum);
						RealNameAnthActivity.this.finish();
					}else{
						UIHelper.showToast(RealNameAnthActivity.this, model.getInfo());
					}
				}
			}

		});
	}

	private void updateUserInfo(String realName, String idNum) {
		DBOpenHelper helper = new DBOpenHelper(RealNameAnthActivity.this);
		String mobile = helper.getMobile();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "UPDATE HJB_USER  SET real_name='" + realName + "' WHERE mobile='" + mobile + "'";
		String sql1 = "UPDATE HJB_USER  SET id_card='" + idNum + "' WHERE mobile='" + mobile + "'";
		db.execSQL(sql);
		db.execSQL(sql1);
	}
}
